import { Response, AuthOptions } from 'request'
import * as request from 'request-promise';
import { AppConfigContainer } from '../../lib/utils/app.config';
import { UserSession } from '../UserDecorator';
import logger from '../../lib/utils/logger';
import { ResposeJson, RequestHelper, ResponseBody } from '../RequestHelper'

export class CmsService {
    getArticleContent(id: number, callback: (data: ResponseBody | null | undefined) => void) {
        //console.log(UserSession.token)
        if (!UserSession.token) {
            callback(null);
        }

        request.get(AppConfigContainer.get('remote_url') + '/api/services/app/Article/Get?id=' + id, {

            json: true,
            auth: {
                sendImmediately: true,
                bearer: UserSession.token
            }
        }, function (error: any, response: Response, body: any) {
            // console.log(body);
            if (error) {
                logger.error('服务器异常：');
                logger.error(error);
                callback(RequestHelper.handlerResponse(null));
            } else {
                logger.debug('----------------------------------------------------');
                logger.debug(response);
                let result = ResposeJson.fromJS(body);
                let resData = RequestHelper.handlerResponse(result);
                callback(resData);
            }


        }).on('request', function (request) {
            //console.log(request)
        });
        // .catch(function (err) {
        //     logger.error('服务器异常：');
        //     logger.error(err);
        //     callback(null);
        // });
    }
}